<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <%@ include file="../inc/meta.jsp" %>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <%@ include file="../inc/css.jsp" %>
    <%@ include file="../inc/js.jsp" %>
</head>

<body class="childrenBody">
<form class="layui-form" style="margin-top: 20px;" lay-filter="powerForm">
    <div class="layui-form-item layui-row layui-col-xs4">
        <label class="layui-form-label">账号类别</label>
        <div class="layui-input-block">
            <select id="roleList" lay-filter="roleList" class="select" lay-verify="type" name="roleList">
                <option value="2">普通账号</option>
                <option value="3">管理员</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-row layui-col-xs12">
        <label class="layui-form-label">分配权限</label>
        <div class="layui-input-block">
            <input type="checkbox" name="module" value="4" lay-skin="primary" title="上传文件">
            <input type="checkbox" name="module" value="5" lay-skin="primary" title="删除文件">
        </div>
    </div>
    <div class="layui-form-item layui-row layui-col-xs12">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-sm" lay-submit="" lay-filter="saveInfo">保存</button>
            <button type="reset" class="layui-btn layui-btn-sm layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>

<script>
    var form;
    layui.use(['form', 'layer'], function () {
        form = layui.form;
        var layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery;

        // 默认选中普通账号的权限
        loadModuleList();

        function loadModuleList() {
            // 先清空所有选项
            $("input:checkbox[name='module']").each(function () {
                $(this).prop("checked", false);
            });

            var roleId = $("#roleList").val();

            $.post("backend/master/powerSearch", {
                roleId: roleId
            }, function (res) {
                for (var i = 0; i < res.length; i++) {
                    // 遍历选择复选框
                    $("input:checkbox[name='module']").each(function () {
                        if (Number($(this).val()) === Number(res[i])) {
                            $(this).prop("checked", true);
                        }
                    });
                }

                // 重新渲染
                form.render();
            });
        }

        form.on('select(roleList)', function (data) {
            loadModuleList();
        });

        form.on("submit(saveInfo)", function (data) {
            var roleId = $("#roleList").val();
            var ids = '';
            $("input:checkbox[name='module']:checked").each(function (i) { // 遍历name=standard的多选框
                ids += $(this).val() + ',';
            });

            if (ids.length > 0) {
                //弹出loading
                var index = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
                // 实际使用时的提交信息
                $.post("backend/master/savePower", {
                    roleId: roleId,
                    name: $('#name').val(),
                    ids: ids
                }, function (res) {
                    layer.close(index);
                    if (res === 1) {
                        if (null != roleId && roleId !== '') {
                            layer.msg("权限修改成功！");
                        } else {
                            layer.msg("权限添加成功！");
                        }
                    } else {
                        if (null != roleId && roleId !== '') {
                            layer.msg("权限修改失败！");
                        } else {
                            layer.msg("权限添加失败！");
                        }
                    }
                });
                return false;
            } else {
                layer.msg("请至少选择一项");
            }
        });
    });
</script>

</html>